package com.newcoin.chat.service.impl;

import com.newcoin.chat.beans.entity.CircleOfFriendComment;
import com.newcoin.chat.beans.entity.CircleOfFriendLikeEntity;
import com.newcoin.chat.beans.entity.CircleOfFriendSeeEntity;
import com.newcoin.chat.beans.entity.CircleOfFriendsEntity;
import com.newcoin.chat.beans.vo.ResultVo;
import com.newcoin.chat.dao.CircleOfFriendsDao;
import com.newcoin.chat.service.CircleOfFriendsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class CircleOfFriendsServiceImpl implements CircleOfFriendsService {
    @Autowired
    private CircleOfFriendsDao dao;

    /**
     * 发朋友圈
     *
     * @param userId
     * @param content
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo<String> sendCircleOfFriend(Long userId, String type, Integer state, Integer whoCanSee, String content, String file, String seeOrNotSeeUserId) throws Exception {
        log.info("发朋友圈入参userId={},type={},state={},whoCanSee={},content={},file={},seeOrNotSeeUserId={}", userId, type, state, whoCanSee, content, file, seeOrNotSeeUserId);
        ResultVo<String> resultVo = new ResultVo<>();
        CircleOfFriendsEntity circleOfFriendsEntity = new CircleOfFriendsEntity();
        circleOfFriendsEntity.setContext(content);
        circleOfFriendsEntity.setFilePath(file);
        circleOfFriendsEntity.setState(state);
        circleOfFriendsEntity.setType(type);
        circleOfFriendsEntity.setUserId(userId);
        circleOfFriendsEntity.setWhoCanSee(whoCanSee);
        log.info("circleOfFriendsEntity={}", circleOfFriendsEntity);
        int row = dao.addCircleOfFriends(circleOfFriendsEntity);
        log.info("发布朋友圈 row={}", row);
        if (row != 1) {
            resultVo.setMsg("发布失败");
            resultVo.setStatus(409);//朋友圈发布失败
            log.info("resultVo={}", resultVo);
            return resultVo;
        }
        if (whoCanSee != 0 && whoCanSee != 3) {
            CircleOfFriendSeeEntity circleOfFriendSeeEntity = new CircleOfFriendSeeEntity();
            circleOfFriendSeeEntity.setCircleFriendsId(circleOfFriendsEntity.getId());
            circleOfFriendSeeEntity.setState(state);
            circleOfFriendSeeEntity.setSeeOrNotSeeUserId(seeOrNotSeeUserId);
            log.info("circleOfFriendSeeEntity={}", circleOfFriendSeeEntity);
            int row1 = dao.addCircleOfFriendSee(circleOfFriendSeeEntity);
            log.info("添加可见与不可见 row1={}", row1);
            if (row1 != 1) {
                throw new RuntimeException("添加可见与不可见失败");
            }
        }
        resultVo.setStatus(200);
        resultVo.setMsg("发布成功");
        log.info("resultVo={}", resultVo);
        return resultVo;
    }

    /**
     * 删除朋友圈
     *
     * @param userId
     * @param circleFriendsId
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo deleteCircleOfFriend(Long userId, Long circleFriendsId) throws Exception {
        log.info("删除朋友圈入参userId={},circleFriendsId={}", userId, circleFriendsId);
        ResultVo resultVo = new ResultVo();
        int row = dao.deleteCircleOfFriends(userId, circleFriendsId);
        log.info("删除朋友圈 row={}", row);
        if (row != 1) {
            resultVo.setStatus(509);//删除失败
            resultVo.setMsg("删除失败");
            log.info("resultVo={}", resultVo);
            return resultVo;
        }
        resultVo.setStatus(200);
        resultVo.setMsg("删除成功");
        log.info("resultVo={}", resultVo);
        return resultVo;
    }

    /**
     * 点赞
     *
     * @param likeUserId
     * @param circleFriendsId
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo circleOfFriendLike(Long likeUserId, Long circleFriendsId) throws Exception {
        log.info("点赞入参 likeUserId={},circleFriendsId={}", likeUserId, circleFriendsId);
        ResultVo resultVo = new ResultVo();
        CircleOfFriendLikeEntity circleOfFriendLikeEntity = new CircleOfFriendLikeEntity();
        circleOfFriendLikeEntity.setCircleFriendsId(circleFriendsId);
        circleOfFriendLikeEntity.setUserId(likeUserId);
        log.info("circleOfFriendLikeEntity={}", circleOfFriendLikeEntity);
        int row = dao.addCircleOfFriendLike(circleOfFriendLikeEntity);
        log.info("点赞 row={}", row);
        if (row != 1) {
            resultVo.setMsg("点赞失败");
            resultVo.setStatus(509);//点赞失败
            log.info("resultVo={}", resultVo);
            return resultVo;
        }
        resultVo.setStatus(200);
        resultVo.setMsg("点赞成功");
        log.info("resultVo={}", resultVo);
        return resultVo;
    }

    /**
     * 评论
     *
     * @param commentUserId
     * @param circleFriendsId
     * @param content
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo circleOfFriendComment(Long commentUserId, Long circleFriendsId, String content) throws Exception {
        log.info("评论入参 commentUserId={},circleFriendsId={},content={}", circleFriendsId, circleFriendsId, content);
        ResultVo resultVo = new ResultVo();
        CircleOfFriendComment circleOfFriendComment = new CircleOfFriendComment();
        circleOfFriendComment.setCircleFriendsId(circleFriendsId);
        circleOfFriendComment.setUserId(commentUserId);
        circleOfFriendComment.setContext(content);
        log.info("circleOfFriendComment={}", circleOfFriendComment);
        int row = dao.addCircleOfFriendComment(circleOfFriendComment);
        if (row != 1) {
            resultVo.setStatus(509);//评论失败
            resultVo.setMsg("评论失败");
            log.info("resultVo={}", resultVo);
            return resultVo;
        }
        resultVo.setStatus(200);
        resultVo.setMsg("评论成功");
        log.info("resultVo={}", resultVo);
        return resultVo;
    }


}
